import numpy as np
from scipy.optimize import curve_fit

# 定义函数形式：y = b / (x^a)
def func(x, a, b):
    return b / (x ** a)

# 假设的示例数据（替换为你的实际数据）
x_data = np.array([2000, 8000, 32000], dtype=float)
y_data = np.array([20000, 10000, 5000], dtype=float)

# 拟合（初始猜测值可根据数据范围设置）
popt, pcov = curve_fit(func, x_data, y_data, p0=[0, 1])
a_fit, b_fit = popt

print(f"拟合结果：a = {a_fit:.4f}, b = {b_fit:.4f}")

#打印出每个数据点的真实值与预测值
for x, y in zip(x_data, y_data):
    y_pred = func(x, a_fit, b_fit)
    print(f"x = {x:.4f}, y = {y:.4f}, y_pred = {y_pred:.4f}")